Bipartite graphs are graphs that have two (bi-) partitions (-partite) of nodes. Nodes within each partition are not allowed to be connected to one another; rather, they can only be connected to nodes in the other partition.
Bipartite graphs can be useful for modelling relations between two sets of entities. We will explore the construction and analysis of bipartite graphs here.
In [1]:
import networkx as nx
from networkx.algorithms import bipartite
In [2]:
# Initialize the city/person bipartite graph.
B = nx.Graph()
cities = ['Beijing', "Xi'an", 'Vancouver', 'San Francisco', 'Austin', 'Boston'] # populate a list of cities
people = ['Eric', 'Nan'] # populate a list of people's names
B.add_nodes_from(cities, bipartite='cities')
B.add_nodes_from(people, bipartite='people')
edges = [("Eric", "Vancouver"), ("Nan", "Xi'an"), ("Eric", "San Francisco"), ("Nan", 'Boston'), ("Eric", 'Boston'), ("Nan", 'Beijing')] # populate a list of 2-tuples, which are the edges. Each 2-tuple should join one city with one person.
B.add_edges_from(edges)
Explore the graph by going through the following algorithms
In [3]:
# Betweenness Centrality
bipartite.betweenness_centrality(B, cities)
Out[3]:
In [4]:
# Degree Centrality
bipartite.degree_centrality(B, cities)
Out[4]:
Think about it...
Which metric is the better indicator of city popularity? How about how well-travelled an individual is?
A bipartite graph can be projected down to a unipartite graph. The projection joins nodes of the same partition, based on their connections to nodes in the other partition. For example, if A is joined to 1 and B is joined to 1, then A and B will be joined in the uni-partite graph projection.
In [5]:
bipartite.projected_graph(B, people).edges()
Out[5]:
Cities connect people in unique ways. :) In the graph, those who you are connected to are people you can talk with about cities you've been to before, and hopefully it'll kick off some networking.
Think about it...
How would you construct a bipartite graph for the Divvy bike sharing data set?
Give it a shot below.
In [ ]: